/*
 *      kernel.cpp
 *      
 *      Copyright 2009 AzureSky <AzureSky.fedora@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */


#include "kernel.h"

kernel_opt::kernel_opt()
{
}

kernel_opt::~kernel_opt()
{
}

/* int: allow ctl-alt-del to reboot */
int kernel_opt::about_ctrl_alt_del()
{
	int reboot[] = {CTL_KERN,KERN_CTLALTDEL};
	int curr;
	size_t curr_size = sizeof(curr);
	
	int result = sysctl(reboot,2,&curr,&curr_size,0,0);
	
	return (result != -1 ? curr : result);	
}

int kernel_opt::change_ctrl_alt_del(int arg)
{
	int reboot[] = {CTL_KERN,KERN_CTLALTDEL};
	int new_value = arg;
	size_t new_size = sizeof(new_value);
	
	int result = sysctl(reboot,2,0,0,&new_value,new_size);
	if(result == -1){
		return -1;
	}
	
	return about_ctrl_alt_del();
}

/* int: panic timeout */
int kernel_opt::about_panic()
{
	int panic[] = {CTL_KERN,KERN_PANIC};
	int curr;
	size_t curr_size = sizeof(curr);
	
	int result = sysctl(panic,2,&curr,&curr_size,0,0);
	
	return (result != -1 ? curr : result);	
}

int kernel_opt::change_panic(int arg)
{
	int panic[] = {CTL_KERN,KERN_PANIC};
	int new_value = arg;
	size_t new_size = sizeof(new_value);
	
	int result = sysctl(panic,2,0,0,&new_value,new_size);
	if(result == -1){
		return -1;
	}
	
	return about_panic();
}

/* int: Maximum size of a messege */
int kernel_opt::about_msgmax()
{
	int msg_max[] = {CTL_KERN,KERN_MSGMAX};
	int curr;
	size_t curr_size = sizeof(curr);
	
	int result = sysctl(msg_max,2,&curr,&curr_size,0,0);
	
	return (result != -1 ? curr : result);	
}

int kernel_opt::change_msgmax(int arg)
{
	int msg_max[] = {CTL_KERN,KERN_MSGMAX};
	int new_value = arg;
	size_t new_size = sizeof(new_value);
	
	int result = sysctl(msg_max,2,0,0,&new_value,new_size);
	if(result == -1){
		return -1;
	}
	
	return about_msgmax();
}

/* int: Maximum message queue size */
int kernel_opt::about_msgmnb()
{
	int msgmnb[] = {CTL_KERN,KERN_MSGMNB};
	int curr;
	size_t curr_size = sizeof(curr);
	
	int result = sysctl(msgmnb,2,&curr,&curr_size,0,0);
	
	return (result != -1 ? curr : result);	
}

int kernel_opt::change_msgmnb(int arg)
{
	int msgmnb[] = {CTL_KERN,KERN_MSGMNB};
	int new_value = arg;
	size_t new_size = sizeof(new_value);
	
	int result = sysctl(msgmnb,2,0,0,&new_value,new_size);
	if(result == -1){
		return -1;
	}
	
	return about_msgmnb();
}

/* int: msg queue identifiers */
int kernel_opt::about_msgmni()
{
	int msgmni[] = {CTL_KERN,KERN_MSGMNI};
	int curr;
	size_t curr_size = sizeof(curr);
	
	int result = sysctl(msgmni,2,&curr,&curr_size,0,0);
	
	return (result != -1 ? curr : result);	
}

int kernel_opt::change_msgmni(int arg)
{
	int msgmni[] = {CTL_KERN,KERN_MSGMNI};
	int new_value = arg;
	size_t new_size = sizeof(new_value);
	
	int result = sysctl(msgmni,2,0,0,&new_value,new_size);
	if(result == -1){
		return -1;
	}
	
	return about_msgmni();
}

/* long: Maximum shared memory segment */
long kernel_opt::about_shmmax()
{
	int shmmax[] = {CTL_KERN,KERN_SHMMAX};
	long curr;
	size_t curr_size = sizeof(curr);
	
	int result = sysctl(shmmax,2,&curr,&curr_size,0,0);
	
	return (result != -1 ? curr : result);	
}

long kernel_opt::change_shmmax(long arg)
{
	int shmmax[] = {CTL_KERN,KERN_SHMMAX};
	long new_value = arg;
	size_t new_size = sizeof(new_value);
	
	int result = sysctl(shmmax,2,0,0,&new_value,new_size);
	if(result == -1){
		return -1;
	}
	
	return about_shmmax();
}

/* int: Maximum size of shared memory */
int kernel_opt::about_shmall()
{
	int shmall[] = {CTL_KERN,KERN_SHMALL};
	int curr;
	size_t curr_size = sizeof(curr);
	
	int result = sysctl(shmall,2,&curr,&curr_size,0,0);
	
	return (result != -1 ? curr : result);	
}

int kernel_opt::change_shmall(int arg)
{
	int shmall[] = {CTL_KERN,KERN_SHMALL};
	int new_value = arg;
	size_t new_size = sizeof(new_value);
	
	int result = sysctl(shmall,2,0,0,&new_value,new_size);
	if(result == -1){
		return -1;
	}
	
	return about_shmall();
}

/* int: shm array identifiers */
int kernel_opt::about_shmmni()
{
	int shmmni[] = {CTL_KERN,KERN_SHMMNI};
	int curr;
	size_t curr_size = sizeof(curr);
	
	int result = sysctl(shmmni,2,&curr,&curr_size,0,0);
	
	return (result != -1 ? curr : result);	
}

int kernel_opt::change_shmmni(int arg)
{
	int shmmni[] = {CTL_KERN,KERN_SHMMNI};
	int new_value = arg;
	size_t new_size = sizeof(new_value);
	
	int result = sysctl(shmmni,2,0,0,&new_value,new_size);
	if(result == -1){
		return -1;
	}
	
	return about_shmmni();
}

/* int: Maximum nr of threads in the system */
int kernel_opt::about_threads_max()
{
	int threads[] = {CTL_KERN,KERN_MAX_THREADS};
	int curr;
	size_t curr_size = sizeof(curr);
	
	int result = sysctl(threads,2,&curr,&curr_size,0,0);
	
	return (result != -1 ? curr : result);	
}

int kernel_opt::change_threads_max(int arg)
{
	int threads[] = {CTL_KERN,KERN_MAX_THREADS};
	int new_value = arg;
	size_t new_size = sizeof(new_value);
	
	int result = sysctl(threads,2,0,0,&new_value,new_size);
	if(result == -1){
		return -1;
	}
	
	return about_threads_max();
}
